/* indent:4  tabsize:8  font:fixed-width */

#import "GameCardPileDelegate.h"
#import "../Solitaire/CardSet.subproj/cardset.h"
#import "Pyramid.h"

@implementation GameCardPileDelegate

/*--------------------------------------------------------------------------
|
|    - setDiscardLeft:left discardRight:right
| 
|    returns: (id) self
|
|---------------------------------------------------------------------------
|
|    Set connections to other piles.
|	
\--------------------------------------------------------------------------*/

- setDiscardLeft:left discardRight:right
{
    discardCardPileViewL = left;
    discardCardPileViewR = right;
    return self;
}


/*--------------------------------------------------------------------------
|
|    - (BOOL)draggedPile:aCardPile from:gameCardPileView
| 
|    returns: (BOOL) YES if it is legal to drag the card.
|
|             (BOOL) NO if it is not legal to drag the card.
|
|---------------------------------------------------------------------------
|
|    Called by a GamePileCardView when the user tries to drag a card.
|	
\--------------------------------------------------------------------------*/

- (BOOL)draggedPile: aCardPile from:gameCardPileView
{

    /*-----------------------------------------------------------------------
     *
     * Dragging is only allowed if the pile is not covered.
     *
     *-----------------------------------------------------------------------*/
    if ([gameCardPileView pileCovered:self])
	return NO;
    else
	return YES;
}


/*--------------------------------------------------------------------------
|
|	- removedPile: aCardPile from: gameCardPileView
|
|	returns:		(id)		self
|
|---------------------------------------------------------------------------
|
|	Called by a GamePileCardView after cards have been dragged off 
|	the pile.
|	
\--------------------------------------------------------------------------*/

- removedPile:aCardPile from:aCardPileView
{
    /*-----------------------------------------------------------------------
    |
    |	Since the game piles only contain a single card, we
    |	know that the pile is now empty, so we remove it from
    |	its superview's list of subviews.  This prevents the pile
    |	from getting any more mouse events.
    |
    \----------------------------------------------------------------------*/
    
    [aCardPileView removeFromSuperview];
    [SolGameController() checkForWin];

    return self;
}


/*--------------------------------------------------------------------------
|
|	- (BOOL) canAcceptPile:aCardPile from:sender in:gameCardPileView
|
|	returns:	(BOOL)	YES if card pile will accept dropped cards.
|
|			(BOOL)	NO if this card pile rejects the dropped cards.
|
|---------------------------------------------------------------------------
|
|	Called by a GamePileCardView when the user tries to drop cards on it.
|	
\---------------------------------------------------------------------------*/

- (BOOL)canAcceptPile:aCardPile from:sender in:gameCardPileView
{
    id card1 = [aCardPile cardAt:CS_TOP];
    id card2 = [[gameCardPileView cardPile] cardAt:CS_TOP];

    /*-----------------------------------------------------------------------
    |
    |	Can only accept a card if this pile is not covered by
    |	others, and the two cards add to 13.
    |
    \----------------------------------------------------------------------*/
    if (![gameCardPileView pileCovered:sender])
    {
/* #define PYRAMID_CHEAT_CHEAT_CHEAT */
#ifndef PYRAMID_CHEAT_CHEAT_CHEAT
	if (card1 && card2)
	{
	    if (([card1 value] + [card2 value]) == 11)
		return YES;
	}
#else
    return YES;
#endif

    }
    return NO;
}


/*--------------------------------------------------------------------------
|
|	- acceptPile: aCardPile in: gameCardPileView
|
|	returns:		(id)		self
|
|---------------------------------------------------------------------------
|
|	Called by a GamePileCardView after cards have been successfully 
|	added to the pile as a result of cards being dropped on it.
|	
\---------------------------------------------------------------------------*/

- acceptPile:aCardPile in:gameCardPileView
{
    id leftMatchPile = [discardCardPileViewL cardPile];
    id rightMatchPile = [discardCardPileViewR cardPile];
    id gamePile = [gameCardPileView cardPile];
    id aCard;

    /*-----------------------------------------------------------------------
    |
    |	Remove both cards from the game pile, and move
    |	them to the match piles.  Then remove the game pile
    |	view from it's superviews list, so that it doesn't get
    |	any more mouse events.
    \------------------------------------------------------------------------*/

    aCard = [gamePile cardAt:CS_TOP];
    [gamePile removeCard:aCard];
    [leftMatchPile addCard:aCard];
    aCard = [gamePile cardAt:CS_TOP];
    [gamePile removeCard:aCard];
    [rightMatchPile addCard:aCard];

    [discardCardPileViewL display];
    [discardCardPileViewR display];
    [gameCardPileView display];
    [gameCardPileView removeFromSuperview];

    [SolGameController() checkForWin];
    return self;
}

/*--------------------------------------------------------------------------
|
|    doubleClickedCard:aCard in:aCardPileView
|
|    returns: (id) self
|
|---------------------------------------------------------------------------
|
|    Double clicked on a card; discard if it is a King.
|	
\--------------------------------------------------------------------------*/

- doubleClickedCard:aCard in:aCardPileView
{
    id cardPile = [aCardPileView cardPile];
    
    // can't remove a card that's covered
    if ([aCardPileView pileCovered:self])
    {
        return self;
    }

    if (aCard && (aCard == [cardPile cardAt:CS_TOP]) && 
                                               [aCard value] == CS_KING)
    {
        [[discardCardPileViewL cardPile] addCard:[cardPile cardAt:CS_TOP]];
	[cardPile removeCard:[cardPile cardAt:CS_TOP]];
	[aCardPileView display];
        [aCardPileView removeFromSuperview];
	[discardCardPileViewL display];
	[SolGameController() checkForWin];

    }
    return self;
}

@end
